<template>
  <div id="customers" class="card col-12 px-0 mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item ms-1" aria-current="page">客户</li>
        <li class="breadcrumb-item active" aria-current="page">列表</li>

        <li class="breadcrumb-item ms-auto">
          <router-link to="/h5/customer/departments">部门信息</router-link>
        </li>
        <li class="breadcrumb-item">
          <router-link to="/h5/passengers">员工信息</router-link>
        </li>
      </ol>
    </nav>
    <template v-if="showSearchForm">
      <div class="card-body">
        <form>
          <div class="row mb-2">
            <input
              type="textfield"
              class="form-control"
              placeholder="公司名称/客户编号/排序拼音"
              name="sc.name"
              v-model.trim="name"
              size="15"
            />
          </div>
          <div class="row mb-2">
            <select
              class="form-select"
              v-model.number="enterpriseType"
            >
              <option value="-1">所有</option>
              <option value="0">企业散客</option>
              <option value="1">企业客户</option>
            </select>
          </div>
          <div class="row mb-2">
            <label>排序</label>
            <select class="form-select" v-model.number="orderBy">
              <option value="0">默认</option>
              <option value="1">协议截止日期（倒序）</option>
              <option value="3">最近购买时间（倒序）</option>
            </select>
          </div>
          <div class="row mb-2">
            <label>状态</label>
            <select class="form-select" v-model.number="status">
              <option value="-1">全部</option>
              <option value="0">停用</option>
              <option value="1">启用</option>
            </select>
          </div>
          <div class="row mb-2">
            <my-date-picker
              id="endDate"
              v-model="endDate"
              placeholder="协议截止日期"
              sizing="sm"
            ></my-date-picker>
          </div>
          <div class="row mb-2">
            <label>记录数</label>
            <select class="form-select" v-model.number="sc.pageSize">
              <option value="10">10</option>
              <option value="25">25</option>
              <option value="50">50</option>
              <option value="100">100</option>
              <option value="200">200</option>
            </select>
          </div>
          <div class="row mb-2">
            <button type="button" class="btn btn-primary mb-2" @click="search()">
              确定
            </button>
            <button
              type="button"
              class="btn btn-secondary"
              title="清空查找条件"
              @click="reset()"
            >
              重置
            </button>
          </div>
        </form>
      </div>
    </template>
    <template v-else>
      <div class="card-header">
        <!-- <router-link class="btn btn-success ms-auto" to="/h5/new/flt/order">新建</router-link> -->
        <router-link to="/customer" class="btn btn-success ms-auto"
          >新建</router-link
        >
        <button
          type="button"
          class="btn btn-primary float-end"
          @click.stop="showSearchOption()"
        >
          查找
        </button>
      </div>
      <table class="table table-hover table-sm">
        <thead>
          <tr>
            <th>公司名称</th>
            <th class="d-none d-md-table-cell">客户编号</th>
            <th class="d-none d-md-table-cell">排序拼音</th>
            <th>联系人</th>
            <th class="d-none d-md-table-cell">电话</th>
            <th class="d-none d-md-table-cell">结算方式</th>
            <th class="d-none d-md-table-cell">结算周期</th>
            <th class="d-none d-md-table-cell">审核？</th>
            <th class="d-none d-md-table-cell">协议截止日期</th>
            <th class="d-none d-md-table-cell">最近购买时间</th>
            <th class="d-none d-md-table-cell">状态</th>
            <th class="text-end"></th>
            <th class="text-end"></th>
          </tr>
        </thead>
        <tbody>
          <tr
            v-for="info in dataList"
            :key="info.id"
            class="text-black"
          >
            <td>
              <span
                class="d-inline-block d-md-none text-truncate"
                style="max-width: 150px"
              >
                {{ info.vipName }}
              </span>
               <span
                class="d-none d-md-inline-block "
              >
                {{ info.vipName }}
              </span>
              
              <span class="text-danger small" v-if="info.status === 4 || info.status === 0"
                >已停用</span
              >
              <span class="text-danger small" v-else-if="info.enterpriseType === 0"
                >散客</span
              >
            </td>
            <td class="d-none d-md-table-cell">{{ info.customerCode }}</td>
            <td class="small d-none d-md-table-cell">{{ info.pinyin }}</td>
            <td>{{ info.linkman }}</td>
            <td class="d-none d-md-table-cell">{{ info.phone }}</td>
            <td class="d-none d-md-table-cell">
              {{ getSettlementTypeDesc(info.settlementType) }}
              <span
                class="small"
                v-if="
                  info.settlementMode !== null && info.settlementMode.length > 0
                "
                >({{ info.settlementMode }})</span
              >
            </td>
            <td class="d-none d-md-table-cell">{{ info.settlementPeriod }}</td>
            <td>
              <span v-if="info.approvalTicket === 1">是</span>
            </td>
            <td class="d-none d-md-table-cell">{{ info.agreementEndDate }}</td>
            <td class="d-none d-md-table-cell">{{ info.recency }}</td>
            <td class="d-none d-md-table-cell">
              <span v-if="info.status === 0" class="text-danger"> 停用 </span>
              <span v-if="info.status === 1" class="text-success"> 启用 </span>
            </td>
            <td>
              <router-link :to="`/customer/` + info.id">详情</router-link>
            </td>
            <td>
              <router-link :to="`/customer?id=` + info.id">修改</router-link>
            </td>
          </tr>
        </tbody>
      </table>
      <nav id="pagination-box" class="float-end">
        <my-pagination
          name="pagination"
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </nav>
    </template>
  </div>
</template>

<script>
import {
  createCustomer,
  searchCustomerDetails,
  getSettlementTypeDesc,
  getApprovalTypeDesc,
  getItineraryTypeDesc,
} from "@/api/customer.js";
import MyPagination from "@/components/my-pagination.vue";
import MyDatePicker from "@/components/my-datepicker.vue";

export default {
  name: "Customers",
  components: {
    MyPagination,
    MyDatePicker,
  },
  data() {
    return {
      showSearchForm: false,
      dataList: [],
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      name: "",
      enterpriseType: -1,
      orderBy: 3,
      status: -1,
      endDate: "",
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (msg) {
      this.$store.dispatch("showLoading", { loadingText: msg });
    },
    hideLoading: function () {
      this.$store.dispatch("hideLoading");
    },
    showSearchOption: function () {
      this.showSearchForm = true;
    },
    search: function () {
      this.showSearchForm = false;
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.name": this.name,
        "sc.enterpriseType": this.enterpriseType,
        "sc.orderBy": this.orderBy,
        "sc.status": this.status,
        "sc.endDate": this.endDate,
      };
      this.showLoading("查询中...");
      searchCustomerDetails(
        params,
        (jsonResult) => {
          this.dataList = jsonResult.dataList;
          this.sc = jsonResult.page;
        },
        () => {
          this.hideLoading();
        }
      );
    },
    reset: function () {
      this.name = "";
      this.endDate = "";
      this.sc.pageNo = 1;
      this.search();
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
    getSettlementTypeDesc: function (val) {
      return getSettlementTypeDesc(val);
    },
    getApprovalTypeDesc: function (val) {
      return getApprovalTypeDesc(val);
    },
    getItineraryTypeDesc: function (val) {
      return getItineraryTypeDesc(val);
    },
  },
};
</script>